package com.ymatou.iapi.settlement.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.settlement.parameter.QueryAdSettlementInfoReq;
import com.ymatou.iapi.settlement.parameter.resp.AdSettlementInfoVo;
import com.ymatou.iapi.settlement.service.QueryAdSettlementInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;

/**
 * 查询广告费率信息接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_QueryAdSettlementInfo {
	private static QueryAdSettlementInfoReq queryAdSettlementInfoReq;
	private static QueryAdSettlementInfoCall queryAdSettlementInfoCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询广告费率信息接口");
	}

	@Before
	public void caseUp() {
		queryAdSettlementInfoReq = new QueryAdSettlementInfoReq();
		queryAdSettlementInfoCall = new QueryAdSettlementInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryAdSettlementInfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@SuppressWarnings("rawtypes")
	private void verifyDb() {

		List<AdSettlementInfoVo> adSettlementInfoVoList = queryAdSettlementInfoCall
				.getResults().getResults();

		for (AdSettlementInfoVo ad : adSettlementInfoVoList) {

			int adid = ad.getAdId();

			List<Map> maps = new DefaultWapper()
					.executeSql("select COUNT(DISTINCT a.OrderId) AS 'orderCount',"
							+ "SUM(a.CostAmount) AS 'adCostAmount'  from Ymt_AdRateInfo a,Ymt_SettlementInvoice s where a.orderid=s.orderid and s.SettlementStatus=1 and s.BusinessType=13 and a.adid="
							+ adid);

			Map map = maps.get(0);

			Logger.verifyEquals(map.get("orderCount"), ad.getOrderCount(),
					"校验orderCount");
			DataUtil.verifyDateEquals((BigDecimal) map.get("adCostAmount"),
					ad.getAdCostAmount(), "校验adCostAmount");
		}

	}
	
	@SuppressWarnings("rawtypes")
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryAdSettlementInfo_001() {
		Logger.start(true, "根据广告单个id查询广告费用结算结果成功");
		try {

			List<Map> maps = new DefaultWapper()
					.executeSql("select top 10 adid from Ymt_AdRateInfo");

			Map map = maps.get(new Random().nextInt(9));

			Integer adid = (Integer) map.get("adid");

			queryAdSettlementInfoReq.setAdIdList(Arrays.asList(adid));

			queryAdSettlementInfoCall.setData(queryAdSettlementInfoReq);

			queryAdSettlementInfoCall.callService();

			verifyDb();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@SuppressWarnings("rawtypes")
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryAdSettlementInfo_002() {
		Logger.start(true, "多个广告id查询广告费用结算结果成功");
		try {

			List<Map> maps = new DefaultWapper()
					.executeSql("select top 10 adid from Ymt_AdRateInfo");

			List<Integer> list = new ArrayList<Integer>();
			maps.forEach(m -> {

				list.add((Integer) m.get("adid"));

			});

			queryAdSettlementInfoReq.setAdIdList(list);

			queryAdSettlementInfoCall.setData(queryAdSettlementInfoReq);

			queryAdSettlementInfoCall.callService();

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryAdSettlementInfo_003() {
		Logger.start(false, "广告list为空");
		try {

			queryAdSettlementInfoCall.setData(queryAdSettlementInfoReq);

			queryAdSettlementInfoCall.callService();

			Logger.verifyEquals("400",
					queryAdSettlementInfoCall.getString("code"), "校验code");

			Logger.verifyEquals("adIdList:广告ID列表不能为空",
					queryAdSettlementInfoCall.getString("msg"), "校验msg");

			Logger.verifyEquals("100",
					queryAdSettlementInfoCall.getString("bcode"), "校验bcode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryAdSettlementInfo_004() {
		Logger.start(false, "广告list的数量大于100条");
		try {

			List<Integer> list = new ArrayList<Integer>();

			for (int i = 0; i < 101; i++) {
				list.add(i);

			}

			queryAdSettlementInfoReq.setAdIdList(list);

			queryAdSettlementInfoCall.setData(queryAdSettlementInfoReq);

			queryAdSettlementInfoCall.callService();

			Logger.verifyEquals("400",
					queryAdSettlementInfoCall.getString("code"), "校验code");

			Logger.verifyEquals(true, queryAdSettlementInfoCall
					.getString("msg").contains("请求广告ID数量超过100个"), "校验msg");

			Logger.verifyEquals("100",
					queryAdSettlementInfoCall.getString("bcode"), "校验bcode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
